// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Monitor Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 13 : TYPE_MONITOR
public alias saveRAM[290] : saved.randomMonitor

// Function declarations
reserve function Monitor_DebugDraw
reserve function Monitor_DebugSpawn
reserve function Monitor_SolidBox

// Static Values
public value Monitor_value24 = 0;

// Tables
public table Monitor_table15
	1, 2, 3, 4, 5, 12, 1, 1, 2, 1
end table

public table Monitor_table14
	1, 2, 3, 4, 5, 9, 10, 11, 12, 1
end table

function Monitor_DebugDraw
	DrawSprite(0)
	temp0 = object[0].value17
	temp0 -= Monitor_value24
	if temp0 == 5
		temp0 += stage.playerListPos
		if temp0 == 10
			temp0 = 5
		end if
	else
		if temp0 > 5
			temp0 += 2
		end if
	end if
	if temp0 > 12
		CheckCurrentStageFolder("Zone04")
		if checkResult == 0
			temp0++
		end if
	end if
	DrawSprite(temp0)
end function


function Monitor_DebugSpawn
	temp0 = object[0].value17
	temp0 -= Monitor_value24
	if temp0 == 5
		temp0 += stage.playerListPos
		if temp0 == 10
			temp0 = 5
		end if
	else
		if temp0 > 5
			temp0 += 2
		end if
	end if
	if temp0 > 12
		CheckCurrentStageFolder("Zone04")
		if checkResult == 0
			temp0++
		end if
	end if
	CreateTempObject(TypeName[Monitor], temp0, object.xpos, object.ypos)
end function

function Monitor_SolidBox
	CheckEqual(object[currentPlayer].animation, 46)
	temp7 = checkResult
	CheckEqual(stage.playerListPos, 7)
	temp7 &= checkResult
	if temp7 == 0
		temp0 = object[currentPlayer].xpos
		temp1 = object[currentPlayer].ypos
		BoxCollisionTest(C_BOX, object.entityPos, -15, -14, 15, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		temp2 = object[currentPlayer].xpos
		object[currentPlayer].xpos = temp0
		object[currentPlayer].ypos = temp1
		switch checkResult
		case 4
			if object[currentPlayer].gravity == GRAVITY_AIR
				object.state = 1
				object.yvel = -0x20000
				object[currentPlayer].yvel = 0x20000
			end if
		case 2
			temp2 += 0x10000
			object[currentPlayer].xpos = temp2		
			BoxCollisionTest(C_BOX, object.entityPos, -15, -10, 15, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 0
				object[currentPlayer].xpos = temp0
			end if
			break
		case 3
			temp2 -= 0x10000
			object[currentPlayer].xpos = temp2		
			BoxCollisionTest(C_BOX, object.entityPos, -15, -10, 15, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 0
				object[currentPlayer].xpos = temp0
			end if
			break
		case 1
			BoxCollisionTest(C_BOX, object.entityPos, -15, -14, 15, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			break
		end switch
	end if	
end function

event ObjectMain
	if object.state == 1
		object.yvel += 0x3800
		object.ypos += object.yvel
		if object.yvel >= 0
			ObjectTileCollision(CSIDE_FLOOR, 0, 16, 0)
			if checkResult == 1
				object.yvel = 0
				object.state = 0
			end if
		end if
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)

		CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
		temp7 = checkResult
		CheckLower(object[0].value1, 20)
		temp7 &= checkResult
		if temp7 == 0	
			CheckEqual(object[currentPlayer].animation, 46)
			temp7 = checkResult
			CheckEqual(stage.playerListPos, 7)
			temp7 &= checkResult
			if temp7 == 0	
				CheckGreater(object[currentPlayer].yvel, -1)
				temp0 = checkResult

				CheckEqual(object[currentPlayer].gravity, GRAVITY_GROUND)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_HAMMER_SPEEN)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_HAMMER_JUMP)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleYuriBladeAir)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleYuriBladeGround)
				temp0 |= checkResult
			else
				temp0 = 1	
			end if	
			if temp0 == 1
				CheckEqual(object[currentPlayer].animation, ANI_JUMPING)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_GLIDING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_GLIDING_STOP)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_HAMMER_SPEEN)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_HAMMER_JUMP)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
				temp0 |= checkResult

				CheckEqual(object[currentPlayer].animation, 46)
				temp7 = checkResult
				CheckEqual(stage.playerListPos, 7)
				temp7 &= checkResult
				if temp7 == 1
					temp0 = 1
				end if	

				if temp0 == 1
					if object[currentPlayer].value16 == 0
						BoxCollisionTest(C_TOUCH, object.entityPos, -15, -14, 15, 16, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
						if checkResult == 1
							object.state = 0
			
							CreateTempObject(TypeName[Smoke Puff], 0, object.xpos, object.ypos)
							object[tempObjectPos].drawOrder = 4

							if object[0].state == PlayerObject_HandleMonikaTeleport
								object[currentPlayer].animation = ANI_JUMPING
								object[currentPlayer].yvel = -0xE0000
								object[currentPlayer].ypos = object.ypos
								object[currentPlayer].ypos -= 0x50000
								object[currentPlayer].gravity = GRAVITY_AIR			
								object[currentPlayer].state = PlayerObject_HandleAir
							else
								CheckEqual(object[currentPlayer].animation, 46)
								temp7 = checkResult
								CheckEqual(stage.playerListPos, 7)
								temp7 &= checkResult
								if temp7 == 0							
									object[currentPlayer].yvel += object[currentPlayer].value25
									object[currentPlayer].yvel += object[currentPlayer].value25
									if object[currentPlayer].animation != ANI_HAMMER_JUMP
										FlipSign(object[currentPlayer].yvel)
									end if			
								end if			
							end if									
							object.type = TypeName[Broken Monitor]
							if object.priority != PRIORITY_XBOUNDS_DESTROY
								object.priority = PRIORITY_ACTIVE
							end if
							object.alpha = 255
							object.value0 = object.ypos
							object.value1 = -0x30000
							PlaySfx(SfxName[Destroy], 0)
							if object.propertyValue == 14
								Rand(temp0, 10)
								if saved.elemental == 1
									GetTableValue(object.propertyValue, temp0, Monitor_table14)
								else
									GetTableValue(object.propertyValue, temp0, Monitor_table15)
								end if
								if object.propertyValue == 5
									if stage.playerListPos == 1
										object.propertyValue = 6
									end if
									
									if stage.playerListPos == 2
										object[arrayPos0].propertyValue = 7
									end if
								end if
							end if	
						end if
					else
						CallFunction(Monitor_SolidBox)
					end if
				else
					CallFunction(Monitor_SolidBox)
				end if
			else
				CallFunction(Monitor_SolidBox)
			end if
		else

		end if	
	next
end event


event ObjectDraw
	DrawSprite(0)
	if object.propertyValue == 17
		temp0 = StageSetup_value22
		temp0 >>= 1
		temp0 %= 3
		switch temp0
			case 0
				DrawSprite(17)
				break
			case 1
				DrawSprite(18)
				break
			case 2
				DrawSprite(19)
				break
		end switch
	else
		switch StageSetup_value22
		case 0
		case 1
			DrawSprite(17)
			break
		case 6
		case 7
			DrawSprite(18)
			break
		case 12
		case 13
			DrawSprite(19)
			break
		default
			DrawSprite(object.propertyValue)
		end switch
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-16, -16, 32, 32, 18, 1)
	SpriteFrame(-8, -9, 16, 14, 51, 16)
	SpriteFrame(-8, -9, 16, 14, 68, 16)
	SpriteFrame(-8, -9, 16, 14, 51, 31)
	SpriteFrame(-8, -9, 16, 14, 68, 31)
	if options.gameMode == 3
		SpriteFrame(-8, -9, 16, 14, 166, 118)
		SpriteFrame(-8, -9, 16, 14, 166, 118)
		SpriteFrame(-8, -9, 16, 14, 166, 118)
	else
		switch stage.playerListPos 
		case 5
			SpriteFrame(-8, -9, 16, 14, 35, 100)
			break
		case 6
			SpriteFrame(-8, -9, 16, 14, 117, 86)
			break
		case 7
			SpriteFrame(-8, -9, 16, 14, 117, 101)
			break
		case 8
			SpriteFrame(-8, -9, 16, 14, 117, 116)
			break
		case 9
			SpriteFrame(-8, -9, 16, 14, 133, 116)
			break
		default	
			SpriteFrame(-8, -9, 16, 14, 51, 46)
			break
		end switch
		SpriteFrame(-8, -9, 16, 14, 68, 46)
		SpriteFrame(-8, -9, 16, 14, 51, 61)
	end if		
	SpriteFrame(-8, -9, 16, 14, 68, 61)
	SpriteFrame(-8, -9, 16, 14, 51, 91)
	SpriteFrame(-8, -9, 16, 14, 68, 76)
	SpriteFrame(-8, -9, 16, 14, 51, 76)
	SpriteFrame(-8, -9, 16, 14, 68, 91)
	SpriteFrame(-8, -9, 16, 14, 18, 100)
	SpriteFrame(-8, -9, 16, 14, 117, 26)
	SpriteFrame(-8, -9, 16, 14, 117, 41)
	SpriteFrame(-8, -9, 16, 14, 117, 56)
									 
	SpriteFrame(-8, -9, 16, 14, 51, 1)
	SpriteFrame(-8, -9, 16, 14, 68, 1)
	SpriteFrame(-8, -9, 16, 14, 117, 71)
	temp0 = 0
	Monitor_value24 = DebugMode_ObjCount
	Monitor_value24--
	while temp0 < 16
		temp2 = 1
		if temp0 >= 4
			if temp0 <= 6
				temp1 = temp0
				temp1 -= 4
				temp3 = stage.playerListPos
				if temp3 == 5
					temp3 = 0
				end if
				if temp3 != temp1
					temp2 = 0
				end if
			end if
		end if
		if temp0 == 12
			CheckCurrentStageFolder("Zone04")
			if checkResult == 0
				temp2 = 0
			end if
		end if
		if temp2 == 1
			SetTableValue(TypeName[Monitor], DebugMode_ObjCount, DebugMode_TypesTable)
			SetTableValue(Monitor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
			SetTableValue(Monitor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
			DebugMode_ObjCount++
		end if
		temp0++
	loop
	CheckEqual(saved.elemental, 0)
	temp0 = checkResult
	CheckEqual(options.attractMode, 1)
	temp0 |= checkResult
	if temp0 == 1
		if stage.activeList == BONUS_STAGE
			if stage.listPos == 0 //Boss Rush
				switch object[arrayPos0].propertyValue
				case 9
					object[arrayPos0].propertyValue = 2
					break
				case 10
				case 11
					object[arrayPos0].type = TypeName[Blank Object]
					break
				end switch
			end if
		else
			switch stage.listPos
			case 12
			case 13
			case 17
				foreach (TypeName[Monitor], arrayPos0, ALL_ENTITIES)
					if object[arrayPos0].propertyValue >= 9
						if object[arrayPos0].propertyValue <= 11
							object[arrayPos0].type = TypeName[Blank Object]
						end if
					end if
				next
				break
			default
				foreach (TypeName[Monitor], arrayPos0, ALL_ENTITIES)
					if object[arrayPos0].propertyValue >= 9
						if object[arrayPos0].propertyValue <= 11
							object[arrayPos0].propertyValue = 2
						end if
					end if
				next
			end switch
		end if
	end if
	switch options.gameMode
		case 2
			foreach (TypeName[Monitor], arrayPos0, ALL_ENTITIES)
				//if object[arrayPos0].propertyValue == 5
				switch object[arrayPos0].propertyValue
					case 5
					case 6
					case 7
						object[arrayPos0].propertyValue = 1
				end switch
				//end if
			next
			break
		case 3
			break
		default
			if saved.lives == 1
				foreach (TypeName[Monitor], arrayPos0, ALL_ENTITIES)
					switch object[arrayPos0].propertyValue
						case 5
						case 6
						case 7
							object[arrayPos0].propertyValue = 1
					end switch
				next
			else
				if stage.playerListPos == 1
					foreach (TypeName[Monitor], arrayPos0, ALL_ENTITIES)
						if object[arrayPos0].propertyValue == 5
							object[arrayPos0].propertyValue = 6
						end if
					next
				end if
				if stage.playerListPos == 2
					foreach (TypeName[Monitor], arrayPos0, ALL_ENTITIES)
						if object[arrayPos0].propertyValue == 5
							object[arrayPos0].propertyValue = 7
						end if
					next
				end if
			end if
			break
	end switch
	if saved.randomMonitor == 1
		if options.attractMode == 0
			if options.gameMode < 2 
				foreach (TypeName[Monitor], arrayPos0, ALL_ENTITIES)
					object[arrayPos0].propertyValue = 14
				next
			end if
		end if
	end if
end event

event RSDKDraw
	DrawSprite(0)
	DrawSprite(object.propertyValue)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-16, -16, 32, 32, 18, 1)
	SpriteFrame(-8, -9, 16, 14, 51, 16)
	SpriteFrame(-8, -9, 16, 14, 68, 16)
	SpriteFrame(-8, -9, 16, 14, 51, 31)
	SpriteFrame(-8, -9, 16, 14, 68, 31)
	SpriteFrame(-8, -9, 16, 14, 51, 46)
	SpriteFrame(-8, -9, 16, 14, 68, 46)
	SpriteFrame(-8, -9, 16, 14, 51, 61)
	SpriteFrame(-8, -9, 16, 14, 68, 61)
	SpriteFrame(-8, -9, 16, 14, 51, 91)
	SpriteFrame(-8, -9, 16, 14, 68, 76)
	SpriteFrame(-8, -9, 16, 14, 51, 76)
	SpriteFrame(-8, -9, 16, 14, 68, 91)
	SpriteFrame(-8, -9, 16, 14, 18, 100)
	SpriteFrame(-8, -9, 16, 14, 117, 26)
	SpriteFrame(-8, -9, 16, 14, 117, 41)
	SpriteFrame(-8, -9, 16, 14, 117, 56)
									 
	SpriteFrame(-8, -9, 16, 14, 51, 1)
	SpriteFrame(-8, -9, 16, 14, 68, 1)
	SpriteFrame(-8, -9, 16, 14, 117, 71)
end event
